//
//  DAMessageDetailTableViewCell.m
//  DAKnowledge
//
//  Created by DreamArts on 16/7/8.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DAMessageDetailTableViewCell.h"
#import "DALabel.h"
#import "Masonry.h"
#import "DAHeader.h"
#import "Utility.h"
#import "DAReplyModel.h"

@interface DAMessageDetailTableViewCell (){
    DALabel *replyUserNameLabel;
    DALabel *replyMessageLabel;
    DALabel *timeLabelLabel;
}
@end
@implementation DAMessageDetailTableViewCell

- (void)awakeFromNib {
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        replyUserNameLabel = [[DALabel alloc]init];
        replyUserNameLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:replyUserNameLabel];
        
        replyMessageLabel = [[DALabel alloc]init];
        replyMessageLabel.font = [UIFont systemFontOfSize:12];
        replyMessageLabel.textColor = [UIColor colorWithHexString:bookNameColor];
        [self.contentView addSubview:replyMessageLabel];
        
        timeLabelLabel = [[DALabel alloc]init];
        timeLabelLabel.font = [UIFont systemFontOfSize:12];
        timeLabelLabel.verticalAlignment = VerticalAlignmentBottom;
        timeLabelLabel.textColor = [UIColor colorWithHexString:timeColor];
        [self.contentView addSubview:timeLabelLabel];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [replyUserNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    [replyMessageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(replyUserNameLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(replyUserNameLabel);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(timeLabelLabel.mas_top).offset(-5);
    }];
    
    [timeLabelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-8);
        make.left.mas_equalTo(replyUserNameLabel);
        make.right.mas_equalTo(-20);
    }];
}

- (void)setReplyModel:(DAReplyModel *)replyModel{
    _replyModel = replyModel;
    
    NSString *str;
    NSString *replyUserName = replyModel.replyUserName;
    NSString *replyToName = replyModel.replyToName;
    if ([replyModel.replyToName isKindOfClass:[NSNull class]] || replyModel.replyToName == nil) {
        str = [NSString stringWithFormat:@"%@", replyModel.replyUserName];
        [replyUserNameLabel setText:str fitToContent:YES];
        NSMutableAttributedString *contentColor = [[NSMutableAttributedString alloc]initWithString:str];
        NSRange range;
        range = [str rangeOfString:replyModel.replyUserName];
        [contentColor addAttribute:NSForegroundColorAttributeName value:[UIColor supportGreenColor] range:range];
        replyUserNameLabel.attributedText = contentColor;
    }else{
        NSString *reply = NSLocalizedString(@"reply", nil);
        str = [NSString stringWithFormat:@"%@ %@ %@", replyUserName, reply,replyToName];
        NSMutableAttributedString *contentColor = [[NSMutableAttributedString alloc]initWithString:str];
        NSRange range;
        NSRange range2;
        range =  [str rangeOfString:replyUserName];
        range2 = [str rangeOfString:replyToName];
        [contentColor addAttribute:NSForegroundColorAttributeName value:[UIColor supportGreenColor] range:range];
        [contentColor addAttribute:NSForegroundColorAttributeName value:[UIColor supportGreenColor] range:range2];
        replyUserNameLabel.attributedText = contentColor;
    }
    
    [replyMessageLabel setText:replyModel.replyContent fitToContent:YES];
    [timeLabelLabel setText:replyModel.replyDate fitToContent:YES];
    
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
